<?php

namespace Modules\Car\Entities;

use App\Models\Car;
use App\Models\InsureLogs;
use App\Models\Order\RenewLease;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Car\CarStatistic;
class RecognizeDrivingLicense extends Model
{
    use HasFactory,SoftDeletes;

    protected $guarded = [];

    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }
    public function carSeries(){
        return $this->belongsTo(Car::class,'car_series_id');
    }
    //获取维修
    public function getServiceAttribute(){
        return Service::query()->where('car_id',$this->id)->orderByDesc('id')->first();
    }
//    获取服务
    public function getMaintenanceAttribute(){
        if($this->id){
            return Maintenance::query()->where('car_id',$this->id)->orderByDesc('id')->first();
        }else{
            return 1;
        }
    }
    //待年检
    public function waittingMot(){
        return $this->hasMany(Mot::class,'car_id');
    }
    //待维修
    public function waittingService(){
        return $this->hasMany(Service::class,'car_id');
    }
    //待保养
    public function waittingMaintenance(){
        return $this->hasMany(Maintenance::class,'car_id');
    }
    //获取上次保养记录
    public function waittingMaintenanceLast(){
        return $this->hasOne(Maintenance::class,'car_id')->latest();
    }

    //待保险
    public function waittingInsurance(){
        return $this->hasMany(Insurance::class,'car_id');
    }
    //车辆成本
    public function carCost(){
        return $this->hasOne(Car\CarCost::class,'car_id');
    }
    public function insuranceLog(){
        return $this->hasMany(InsureLogs::class,'car_id');
    }
    // 残值统计
    public function residual(){
        return $this->hasOne(CarStatistic::class,'id','gear_car_id');
    }
    public function orders(){
        return $this->hasMany(CarOrder::class,'gear_car_id','id');
    }
    public function useOrders(){
        return $this->hasMany(CarOrder::class,'gear_car_id','id');
    }
    public function renews(){
        return $this->hasMany(RenewLease::class,'gear_car_id','id');
    }

}
